//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.lry.rpc.starter.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

public abstract class BeanUtils {
    private static final Log logger = LogFactory.getLog(BeanUtils.class);
    private static final String[] EMPTY_BEAN_NAMES = new String[0];

    public BeanUtils() {
    }

    public static boolean isBeanPresent(ListableBeanFactory beanFactory, Class<?> beanClass) {
        return isBeanPresent(beanFactory, beanClass, false);
    }

    public static boolean isBeanPresent(ListableBeanFactory beanFactory, Class<?> beanClass, boolean includingAncestors) {
        String[] beanNames = getBeanNames(beanFactory, beanClass, includingAncestors);
        return !ObjectUtils.isEmpty(beanNames);
    }

    public static boolean isBeanPresent(ListableBeanFactory beanFactory, String beanClassName, boolean includingAncestors) {
        boolean present = false;
        ClassLoader classLoader = beanFactory.getClass().getClassLoader();
        if (ClassUtils.isPresent(beanClassName, classLoader)) {
            Class beanClass = ClassUtils.resolveClassName(beanClassName, classLoader);
            present = isBeanPresent(beanFactory, beanClass, includingAncestors);
        }

        return present;
    }

    public static boolean isBeanPresent(ListableBeanFactory beanFactory, String beanClassName) {
        return isBeanPresent(beanFactory, beanClassName, false);
    }

    public static boolean isBeanPresent(BeanFactory beanFactory, String beanName, Class<?> beanClass) throws NullPointerException {
        return beanFactory.containsBean(beanName) && beanFactory.isTypeMatch(beanName, beanClass);
    }

    public static String[] getBeanNames(ListableBeanFactory beanFactory, Class<?> beanClass) {
        return getBeanNames(beanFactory, beanClass, false);
    }

    public static String[] getBeanNames(ListableBeanFactory beanFactory, Class<?> beanClass, boolean includingAncestors) {
        return includingAncestors ? BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory, beanClass, true, false) : beanFactory.getBeanNamesForType(beanClass, true, false);
    }

    public static Class<?> resolveBeanType(String beanClassName, ClassLoader classLoader) {
        if (!StringUtils.hasText(beanClassName)) {
            return null;
        } else {
            Class beanType = null;

            try {
                beanType = ClassUtils.resolveClassName(beanClassName, classLoader);
                beanType = ClassUtils.getUserClass(beanType);
            } catch (Exception var4) {
                if (logger.isErrorEnabled()) {
                    logger.error(var4.getMessage(), var4);
                }
            }

            return beanType;
        }
    }

    public static <T> T getOptionalBean(ListableBeanFactory beanFactory, Class<T> beanClass, boolean includingAncestors) throws BeansException {
        String[] beanNames = getBeanNames(beanFactory, beanClass, includingAncestors);
        if (ObjectUtils.isEmpty(beanNames)) {
            if (logger.isDebugEnabled()) {
                logger.debug("The bean [ class : " + beanClass.getName() + " ] can't be found ");
            }

            return null;
        } else {
            Object bean = null;

            try {
                bean = includingAncestors ? BeanFactoryUtils.beanOfTypeIncludingAncestors(beanFactory, beanClass) : beanFactory.getBean(beanClass);
            } catch (Exception var6) {
                if (logger.isErrorEnabled()) {
                    logger.error(var6.getMessage(), var6);
                }
            }

            return (T) bean;
        }
    }

    public static <T> T getOptionalBean(ListableBeanFactory beanFactory, Class<T> beanClass) throws BeansException {
        return getOptionalBean(beanFactory, beanClass, false);
    }

    public static <T> T getBeanIfAvailable(BeanFactory beanFactory, String beanName, Class<T> beanType) throws BeansException {
        if (isBeanPresent(beanFactory, beanName, beanType)) {
            return beanFactory.getBean(beanName, beanType);
        } else {
            if (logger.isDebugEnabled()) {
                logger.debug(String.format("The bean[name : %s , type : %s] can't be found in Spring BeanFactory", beanName, beanType.getName()));
            }

            return null;
        }
    }

    public static <T> List<T> getSortedBeans(ListableBeanFactory beanFactory, Class<T> type) {
        Map<String, T> beansOfType = BeanFactoryUtils.beansOfTypeIncludingAncestors(beanFactory, type);
        List<T> beansList = new ArrayList(beansOfType.values());
        AnnotationAwareOrderComparator.sort(beansList);
        return Collections.unmodifiableList(beansList);
    }

    public static <T> Map<String, T> sort(Map<String, T> beansMap) {
        Map<String, T> unmodifiableBeansMap = Collections.unmodifiableMap(beansMap);
        List<BeanUtils.NamingBean<T>> namingBeans = new ArrayList(unmodifiableBeansMap.size());
        Iterator var3 = unmodifiableBeansMap.entrySet().iterator();

        while(var3.hasNext()) {
            Entry<String, T> entry = (Entry)var3.next();
            String beanName = (String)entry.getKey();
            T bean = entry.getValue();
            BeanUtils.NamingBean<T> namingBean = new BeanUtils.NamingBean(beanName, bean);
            namingBeans.add(namingBean);
        }

        AnnotationAwareOrderComparator.sort(namingBeans);
        Map<String, T> sortedBeansMap = new LinkedHashMap(beansMap.size());
        Iterator var9 = namingBeans.iterator();

        while(var9.hasNext()) {
            BeanUtils.NamingBean<T> namingBean = (BeanUtils.NamingBean)var9.next();
            sortedBeansMap.put(namingBean.name, namingBean.bean);
        }

        return sortedBeansMap;
    }

    static class NamingBean<T> extends AnnotationAwareOrderComparator implements Comparable<BeanUtils.NamingBean>, Ordered {
        private final String name;
        private final T bean;

        NamingBean(String name, T bean) {
            this.name = name;
            this.bean = bean;
        }

        public int compareTo(BeanUtils.NamingBean o) {
            return this.compare(this, o);
        }

        public int getOrder() {
            return this.getOrder(this.bean);
        }
    }
}
